﻿using System;
using Framework.MVC;
using Log.Model;
using Extention.Model;
using Extention.Model.Behavior;
using Account.Model;
using Account.Model.Behavior;
using Identity.Model;
using Identity.IService.Entry;
using Common.IService.Entry;
using Extention.IService.Entry;
using Framework.MVVM.Model;
using Common.IService;
using Common.Service;
using Log.IService;
using Log.Service;
using Identity.IService;
using Identity.Service;
using Account.IService;
using Account.Service;


namespace FrontCenter.Models
{
    [ServiceAttribute(typeof(IVerifyCodeService), typeof(VerifyCodeService), "Instance")]
    [ServiceAttribute(typeof(Extention.IService.IOrderService), typeof(Extention.Service.OrderService), "Instance")]
    [ServiceAttribute(typeof(IAccountService), typeof(AccountService), "Instance")]
    [ServiceAttribute(typeof(IIdentityService), typeof(IdentityService), "Instance")]
    [ServiceAttribute(typeof(ILogService), typeof(LogService), "Instance")]
    [ServiceAttribute(typeof(ICodeService), typeof(CodeService), "Instance")]
    public class NewOrderModel : AsyncModelBase, IWriteLog, IValidateVerifyCode,IDeleteVerifyCode, IGetAccount, INewOrder
    {
        #region Field

        private const string m_verifycode_empty = "neworder0";
        private const string m_verifycode_error = "neworder1";
        private const string m_verifycode_complete = "neworder2";
        private const string m_verifyvalue_error = "neworder3";
        private const string m_verifyvalue_complete = "neworder4";
        private const string m_account_error = "neworder5";
        private const string m_account_complete = "neworder6";
        private const string m_order_error = "neworder7";
        private const string m_order_complete = "neworder8";

        private const string m_code = "code";
        private const string m_error = "error";

        private const string m_errors_verifycode = "verifycode";
        private const string m_errors_account = "account";
        private const string m_errors_order = "order";
        #endregion Field

        #region Property

        public string Resource { get; set; }

        #endregion Property

        #region AsyncModelBase

        protected override void OnInit(Type type)
        {
            OnBeginRequest();
            this.ValidateVerifyCode(Resource, m_verifycode_error, m_verifycode_complete);
        }

        #endregion AsyncModelBase

        #region IWriteLog

        public void OnWriteLogComplete(string eventCode)
        {
            OnEndRequest();
        }

        public void OnWriteLogError(string eventCode)
        {
            OnEndRequest();
        }

        #endregion IWriteLog

        #region IValidateVerifyCode

        public string Source { get; set; }

        public string VerifyCode { get; set; }

        public string VerifyValue { get; set; }

        public bool IsOnce { get; set; }

        public void OnValidateVerifyCodeComplete(string key, bool entry)
        {
            this.DeleteVerifyCode(Resource, m_verifycode_error, m_verifycode_complete);
        }

        public void OnValidateVerifyCodeError(string key)
        {
            OnComplete(m_error, m_errors_verifycode);
            this.Log(key, new Identity.IService.Entry.IdentityEntry { code = VerifyCode, value = VerifyValue, source = Source });
        }

        #endregion IValidateVerifyCode

        #region IDeleteVerifyCode

        public void OnDeleteVerifyCodeComplete(string key, bool result)
        {
            this.GetAccount(AccountResource, m_account_error, m_account_complete);
        }

        public void OnDeleteVerifyCodeError(string key)
        {
            this.GetAccount(AccountResource, m_account_error, m_account_complete);
        }

        #endregion IDeleteVerifyCode

        #region IGetAccount

        public string AccountName { get; set; }

        public string AccountResource { get; set; }

        public void OnGetAccountComplete(string key, string code)
        {
            OnBeginRequest();
            OrderUser = code;
            this.NewOrder(m_order_error, m_order_complete);
            this.Log(key, new SourceEntry<string> { source = Source, data = AccountName });
        }

        public void OnGetAccountError(string key)
        {
            OnComplete(m_error, m_errors_account);
            this.Log(key, new SourceEntry<string> { source = Source, data = AccountName });
        }

        #endregion IGetAccount

        #region INewOrder

        public decimal OrderAmount { get; set; }

        public string OrderCode { get; set; }

        public string OrderSource { get; set; }

        public string OrderUser { get; set; }

        public void OnNewOrderComplete(string key, OrderEntry entry)
        {
            OnComplete(m_code, entry.orderCode);
            this.Log(key, new SourceEntry<OrderEntry> { source = Source, data = entry });
        }

        public void OnNewOrderError(string key)
        {
            OnComplete(m_error, m_errors_order);
            this.Log(key, new SourceEntry<OrderEntry> { source = Source, data = new OrderEntry { amount = OrderAmount, owner = OrderUser, orderCode = OrderCode, sourceCode = OrderSource } });
        }

        #endregion INewOrder
    } 
}